package com.wangzhixuan.service.impl;

import java.util.List;
import java.util.Map;

import com.wangzhixuan.commons.constant.ApiCode;
import com.wangzhixuan.commons.utils.PageInfo;
import com.wangzhixuan.model.Rank;
import com.wangzhixuan.mapper.RankMapper;
import com.wangzhixuan.service.IRankService;
import com.baomidou.mybatisplus.plugins.Page;

import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhixuan.wang
 * @since 2017-08-15
 */
@Service
public class RankServiceImpl extends ServiceImpl<RankMapper, Rank> implements IRankService {
	
	@Autowired
    private RankMapper rankMapper;
    
	@Override
    public void selectDataGrid(PageInfo pageInfo) {
    	Page<Map<String, Object>> page = new Page<Map<String, Object>>(pageInfo.getNowpage(), pageInfo.getSize());
        page.setOrderByField(pageInfo.getSort());
        page.setAsc(pageInfo.getOrder().equalsIgnoreCase("asc"));
        List<Rank> list = rankMapper.selectRankPage(page, pageInfo.getCondition());
        pageInfo.setRows(list);
        pageInfo.setTotal(page.getTotal());
    }

	@Override
	public List<Map<String, Object>> selectlist() {
		List<Map<String,Object>> selectlist = rankMapper.selectlist();
		return selectlist;
	}

	@Override
	public Rank selectRanking(Integer userId, Integer workId) {
		Rank selectRanking = rankMapper.selectRanking(userId, workId);
		return selectRanking;
	}
	
	@Override
	public void insertUserOnTime(Integer userId) {
		Rank rank = new Rank();
		rank.setDelaytimeNum(0);
		rank.setFailNum(0);
		rank.setSucceedNum(1);
		rank.setTotalOnTimeNum(1);
		rank.setUserId(userId);
		rankMapper.insert(rank);
	}

	@Override
	public void insertUserDeTime(Integer userId) {
		Rank rank = new Rank();
		rank.setDelaytimeNum(1);
		rank.setFailNum(0);
		rank.setSucceedNum(1);
		rank.setTotalOnTimeNum(0);
		rank.setUserId(userId);
		rankMapper.insert(rank);
	}

	@Override
	public void updateUserOnTime(Integer userId) {
		rankMapper.updateUserOnTime(userId);
	}

	@Override
	public void updateUserDeTime(Integer userId) {
		rankMapper.updateUserDeTime(userId);
	}

	@Override
	public void updateUserFail(Integer userId) {
		rankMapper.updateUserFail(userId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List selectTotalOnTimeNum() {
		List<Integer> list = rankMapper.selectTotalOnTimeNum();
		return list;
	}

	@Override
	public void updateRanking(Integer ranking, Integer totalOnTimeNum) {
		rankMapper.updateRanking(ranking, totalOnTimeNum);
	}

	@Override
	public Boolean autoRank() {
		// TODO Auto-generated method stub
		List<?> list = rankMapper.selectTotalOnTimeNum();
		if(list.size()==0){
			return true;
		}
		else{
			Integer[] rank = (Integer[]) list.toArray(new Integer[0]);
			for(int i=0;i<rank.length;i++){  
			    for(int j=0;j<rank.length-1-i;j++){  
			        if(rank[j] < rank[j+1]){  
			            int temp = rank[j];  
			            rank[j] = rank[j+1];  
			            rank[j+1] = temp;  
			        }   
			    }  
			} 
			for(int j=0;j<rank.length;j++){  
				System.out.println("paiming:"+(j+1));
				if(j != 0){
					if(rank[j]==rank[j-1]){
						continue;
					}
				}
				rankMapper.updateRanking(j+1,rank[j]);
		    }
			return true;
		}
	}
	
	
	
}
